import $ from  'jquery'
export function selectapi() {
    try {
        $("svg").remove()
        var temp={}
        //ajax
        $.ajax({
            type: "GET",
            url: "http://localhost:9598/gselect/v1/rest/getDataByProp",
            data: {msg:$("#t6").val(),step:"1"},
            dataType: "json",
            async:false,//此处需要同步  false=同步   true=异步
            success: function(data){
                console.log("data value")
                console.log(data)
                temp = data
            }
        });

        var data = temp
        // data.nodes = JSON.parse(data).nodes;
        // data.links = JSON.parse(data).links;
        // data.nodes = data.nodes;
        // data.links = data.links;
        var config = {
            //鼠标mouseover后的弹窗
            content: document.getElementById("t5").value,
            contentHook: null,
            //节点配色方案（可为空)
            nodeColor: document.getElementById("t3").value,
            //连接线配色方案（可为空）
            linkColor: document.getElementById("t4").value,
            width: document.getElementById("container").clientWidth,
            height: document.getElementById("container").clientHeight
        }
        initKG(data, config, "#container")
    } catch (err) {
        console.log(err)
        Materialize.toast('渲染存在异常', 2000)
    }
}

var addv = function(circle,xnodes,edges_line,xlinks){

    //新增节点
    var nodenew = {
        "key":"xy",
        "name": "xy",
        "type": "person"
    }
    var linknew ={
        "rela": "朋友",
        "source": "xy",
        "target": "330902196011060646",
        "type": "123"
    }
    //data:nodes 至少需要一个name,name不能相同，否则无法展示
    var nodeDict = xnodes;

    var links = xlinks;

    var nodes = {};
    nodeDict[nodenew.key] = nodenew
    console.log(links)
    linknew.source = nodeDict[linknew.source]
    console.log(nodeDict[linknew.source])
    linknew.target = nodeDict[linknew.target]
    links.push(linknew)

    console.log(links)
    links.forEach(function (link) {
        // console.log(link)
        //利用source和target名称进行连线以及节点的确认
        if(link.source!=undefined && link.target!=undefined){
            link.source = nodeDict[link.source]
            console.log(link.source)
            nodes[link.source.name] = link.source
            link.target = nodeDict[link.target]
            nodes[link.target.name] = link.target
        }

    });
    //
    //
    // force.nodes(d3.values(nodes)).links(links).start()
    // circle.data(force.nodes).enter();
    // edges_line.data(force.links).enter()
}

//node
export var showTable = function (node) {
    $('#xymodal').modal();
    $('#modalcont').html("")
    if(node.type === "events"){
        geteventsdetail(node.key)
    }else if(node.type === "car"){
        showCar(node)
    }

    $('#xymodal').modal("open");
    // $('#xymodal').modal("close");
}

//link
export var showLinkTable = function (link) {
    $('#xymodal').modal();
    $('#modalcont').html("")
    if(link.type === "affiliation" && link.target.type === "events"){
        showeventsaffiliation(link)
    }else if(link.type === "checkin"){
        showcheckin(link)
    }else if(link.type === "cohabit"){
        showcohabit(link)
    }

    $('#xymodal').modal("open");
    // $('#xymodal').modal("close");
}

export var showEvents = function (result) {
    $('#modalcont').html("<div class=\"modal-header\">\n" +
        "                        <h4 class=\"modal-title\">"+result.data[0].ajmc+"</h4>\n" +
        "                        <div class=\"modal-body\">"+result.data[0].jyaq+"</div>\n" +
        "                    </div>")
}

export var showCar = function (result) {
    $('#modalcont').html("<div class=\"modal-header\">\n" +
        "                        <h4 class=\"modal-title\">"+result.key+"</h4>\n" +
        "                        <div class=\"modal-body\">"+result.prop.brand+" "+result.prop.cartype+"</div>\n" +
        "                    </div>")
}

export var showcohabit = function (result) {
    var tab = "<table border='1'>"+"<tr align='center'>"+"<td>"+"人员1"+"</td>"+
        "<td>"+"人员1入住时间"+"</td>"+"<td>"+"人员2"+"</td>"+"<td>"+"人员2入住时间"+"</td>"+"<td>"+"房间号"+"</td>"+"</tr>";
    for(var i=0;i<result.props.length;i++){
        tab = tab+"<tr align='center'>"+"<td>"+result.props[i].srcid+"</td>"+"<td>"+result.props[i].srcidhotelintime+"</td>"+"<td>"+result.props[i].dstid+"</td>"+"<td>"+result.props[i].dstidhotelintime+"</td>"+"<td>"+result.props[i].roomnumber+"</td>"+"</tr>"
    }
    tab = tab+"</table>";

    $('#modalcont').html("<div class=\"modal-header\">\n" +
        "                        <h4 class=\"modal-title\">"+"同住关系详情"+"</h4>\n" +
        "                        <div class=\"modal-body\">"+tab+"</div>\n" +
        "                    </div>")
}

var showeventsaffiliation = function (result) {
    var tab = "<table border='1'>"+"<tr align='center'>"+"<td>"+"报案人"+"</td>"+
        "<td>"+"案件编号"+"</td>"+"<td>"+"案件开始时间"+"</td>"+"<td>"+"案件结束时间"+"</td>"+"</tr>";
    for(var i=0;i<result.props.length;i++){
        tab = tab+"<tr align='center'>"+"<td>"+result.props[i].srcid+"</td>"+"<td>"+result.props[i].dstid+"</td>"+"<td>"+result.props[i].starttime+"</td>"+"<td>"+result.props[i].endtime+"</td>"+"</tr>"
    }
    tab = tab+"</table>";

    $('#modalcont').html("<div class=\"modal-header\">\n" +
        "                        <h4 class=\"modal-title\">"+"人-事件关系详情"+"</h4>\n" +
        "                        <div class=\"modal-body\">"+tab+"</div>\n" +
        "                    </div>")
}

export var showcheckin = function (result) {
    var tab = "<table border='1'>"+"<tr align='center'>"+"<td>"+"人员"+"</td>"+
        "<td>"+"酒店编码"+"</td>"+"<td>"+"入住时间"+"</td>"+"<td>"+"离店时间"+"</td>"+"<td>"+"房间号"+"</td>"+"</tr>";
    for(var i=0;i<result.props.length;i++){
        tab = tab+"<tr align='center'>"+"<td>"+result.props[i].srcid+"</td>"+"<td>"+result.props[i].dstid+"</td>"+"<td>"+result.props[i].hotelintime+"</td>"+"<td>"+result.props[i].hotelouttime+"</td>"+"<td>"+result.props[i].roomnumber+"</td>"+"</tr>"
    }
    tab = tab+"</table>";

    $('#modalcont').html("<div class=\"modal-header\">\n" +
        "                        <h4 class=\"modal-title\">"+"入住关系详情"+"</h4>\n" +
        "                        <div class=\"modal-body\">"+tab+"</div>\n" +
        "                    </div>")
}

function geteventsdetail(key) {
    try {
        //ajax
        $.ajax({
            type: "GET",
            url: "http://localhost:9598/gselect/v1/rest/getEventsDetail",
            data: {eventsid:key},
            dataType: "json",
            async:false,//此处需要同步  false=同步   true=异步
            success: function(data){
                showEvents(data)
            }
        });
    } catch (err) {
        console.log(err)
        Materialize.toast('查询异常', 2000)
    }
}
